package logic

import (
	"context"
	"fmt"
	"home/models"

	"home/homerpc/homerpc"
	"home/homerpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type FurnishedAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFurnishedAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FurnishedAddLogic {
	return &FurnishedAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 添加
func (l *FurnishedAddLogic) FurnishedAdd(in *homerpc.FurnishedAddRequest) (*homerpc.FurnishedAddResponse, error) {
	// todo: add your logic here and delete this line

	var furnished models.Furnished
	models.DB.Debug().Where("name = ?", in.Name).Find(&furnished)
	if furnished.ID != 0 {
		return nil, fmt.Errorf("该家具已存在无法添加")
	}
	furnished = models.Furnished{
		Name:      in.Name,
		Price:     float64(in.Price),
		ClassName: in.ClassName,
		BrandName: in.BrandName,
	}

	models.DB.Debug().Create(&furnished)

	var brand models.Brand
	models.DB.Debug().Where("brandName = ?", in.BrandName).Find(&brand)
	if brand.ID == 0 {
		brand = models.Brand{
			BrandName: in.BrandName,
		}
		models.DB.Debug().Create(&brand)
	} else {
		return nil, nil
	}

	var class models.Class
	models.DB.Debug().Where("className = ?", in.ClassName).Find(&class)
	if class.ID == 0 {
		class = models.Class{
			ClassName: in.ClassName,
		}
		models.DB.Debug().Create(&class)
	} else {
		return nil, nil
	}

	return &homerpc.FurnishedAddResponse{
		FurnishedId: int64(furnished.ID),
	}, nil
}
